﻿using System;
using System.Reactive.Linq;
using DeepEarth.Map.Core;
using DeepEarth.Toolkit.Common;
using DeepEarth.Toolkit.Geometry.Data;

namespace DeepEarth.Toolkit.Controls
{
	internal class PushpinDrawProcessor : DrawingProcessorBase
	{
		IDisposable subscription;

		public PushpinDrawProcessor(MapInteractionControl mapControl)
			:base(mapControl) { }

		protected override void InitializeProcessor()
		{
			subscription = GetSingleClick(Map.AsFrameworkElement)
				.Select(x => Map.ViewportPointToLocation(x))
				.Subscribe(x => OnGeometryProduced(new PushpinGeometry
				                                   {
				                                   	Position = x,
				                                   	PositionOrigin = new PositionOrigin {X = 0.5, Y = 1.0}
				                                   }));
		}

		protected override void CancelProcessor()
		{
			if (subscription != null) subscription.Dispose();
		}
	}
}